package com.universal.schedule.fragments;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.view.Display;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.TextView;

import com.melnykov.fab.FloatingActionButton;
import com.universal.schedule.EditActivity;
import com.universal.schedule.MineActivity;
import com.universal.schedule.R;
import com.universal.schedule.adapter.DiaryAdapter;
import com.universal.schedule.adapter.DiaryAdapter.MyItemClickListener;
import com.universal.schedule.db.DBManager;
import com.universal.schedule.db.DatabaseHelper;
import com.universal.schedule.enity.Diary;

import java.util.ArrayList;
import java.util.List;

public class DiaryFragments extends Fragment {
	private View layout;
	private LayoutInflater mInflater;
	private RecyclerView mRecyleView;
	private List<Diary> data=new ArrayList<Diary>();
	private DiaryAdapter adapter;
	private TextView mView;
	private FloatingActionButton view_setting;
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		initUI(inflater, container);
		return layout;
	}

	private void initUI(LayoutInflater inflater, ViewGroup container) {
		if(layout==null)
		{
			mInflater=inflater;
			layout = inflater.inflate(R.layout.fragments_diary, container, false);
			findview();
			initData();
		}
	}

	private void initData() {
		//使用瀑布流布局,第一个参数 spanCount 列数,第二个参数 orentation 排列方向
		StaggeredGridLayoutManager recyclerViewLayoutManager =
				new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL);
		//线性布局Manager
//        LinearLayoutManager recyclerViewLayoutManager = new LinearLayoutManager(this);
//        recyclerViewLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
		//网络布局Manager
//        GridLayoutManager recyclerViewLayoutManager = new GridLayoutManager(this, 3);
		//给recyclerView设置LayoutManager
		mRecyleView.setLayoutManager(recyclerViewLayoutManager);
		adapter = new DiaryAdapter(data, getActivity());
		//设置adapter
		mRecyleView.setAdapter(adapter);

		adapter.setItemClickListener(new MyItemClickListener() {

			@Override
			public void onItemClick(View view, int position) {
				createDialog(getActivity(),position);

			}
		});
	}

	private void findview() {
		mRecyleView = (RecyclerView) layout.findViewById(R.id.recycler_view);
		FloatingActionButton mfloatButton = (FloatingActionButton) layout.findViewById(R.id.fab);
		mfloatButton.attachToRecyclerView(mRecyleView);
		mfloatButton.setOnClickListener(new OnClickListener() {


			@Override
			public void onClick(View v) {
				Intent intent = new Intent(getActivity(), EditActivity.class);
				startActivityForResult(intent, 0);

			}
		});

		/*mView = (TextView) layout.findViewById(R.id.view);

		mView.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				Intent intent = new Intent(getActivity(), MineActivity.class);
				startActivity(intent);
			}
		});*/

		view_setting = (FloatingActionButton)layout.findViewById(R.id.view_setting);

		view_setting.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				Intent intent = new Intent(getActivity(), MineActivity.class);
				startActivity(intent);
			}
		});
	}

	@Override
	public void onStart() {
		super.onStart();
		data.clear();
		getMessage();
		adapter.notifyDataSetChanged();
	}

	private void getMessage(){
		Cursor mCursor = DBManager.getInstance(getActivity()).selectByTableName(DatabaseHelper.DIARY_TABLE);
		if (mCursor.moveToFirst()) {
			do {
				Diary diary = new Diary(mCursor.getString(2), mCursor.getString(1),mCursor.getString(3));
				data.add(diary);

			} while (mCursor.moveToNext());
		}
	}

	public  void createDialog(final Context mContext,final int position){
		LayoutInflater inflaters = (LayoutInflater)mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		View view = inflaters.inflate(R.layout.dialog_warn_prompt, null);
		Button btnCancle=(Button) view.findViewById(R.id.dialog_cancel);
		Button btnOk=(Button) view.findViewById(R.id.dialog_ok);
		btnCancle.setText("取消");
		btnOk.setText("确认");
		final Dialog mDailog = createWarnDailog(mContext, "日记","是否删除当前日记", view, false);
		mDailog.show();
		btnCancle.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				mDailog.cancel();
			}
		});
		btnOk.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				int deleteByField = DBManager.getInstance(getActivity()).deleteByField(DatabaseHelper.DIARY_TABLE,
						DatabaseHelper.COLUMN_DIARY_ID, String.valueOf(position+1));
				DBManager.getInstance(getActivity()).updateTablePrimaryKey(DatabaseHelper.DIARY_TABLE, DatabaseHelper.COLUMN_DIARY_ID, position+1);
				data.clear();
				getMessage();
				adapter.notifyDataSetChanged();
				mDailog.cancel();
			}
		});
	}

	/** 自定义提示是否确定对话框
	 * @param context  上下文
	 * @param title  标题
	 * @param context  提示内容
	 * @param view   布局视图  The root View of the inflated hierarchy.
	 **/
	public  Dialog createWarnDailog(Context context,String title,String content,View view,boolean canceledOnTouchOutside){

		TextView text=(TextView) view.findViewById(R.id.text_message);
		text.setText(content);
		Button btnCancle=(Button) view.findViewById(R.id.dialog_cancel);
		final Dialog dialog=createDialog(context,title,view,canceledOnTouchOutside);
		btnCancle.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				dialog.cancel();
			}
		});

		return dialog;
	}

	public  Dialog createDialog(Context context,String title, View view,boolean canceledOnTouchOutside){

		TextView titleview=(TextView) view.findViewById(R.id.title);
		titleview.setText(title);
		Dialog dialog = new Dialog(context,R.style.room_dialog);
		Window window = dialog.getWindow();
		// window.setWindowAnimations(R.style.dialogWindowAnim); // 设置窗口弹出动画
		window.setBackgroundDrawableResource(android.R.color.transparent);
		window.setGravity(Gravity.CENTER);
		dialog.setContentView(view);//添加布局视图
		dialog.setCanceledOnTouchOutside(canceledOnTouchOutside);

		WindowManager m = ((Activity) context).getWindowManager();
		Display d = m.getDefaultDisplay();
		WindowManager.LayoutParams p = window.getAttributes();
		p.width = (int) (d.getWidth() )-dip2px(context, 50);
		window.setAttributes(p);
		return dialog;
	}

	public static int dip2px(Context context, float dpValue) {
		final float scale = context.getResources().getDisplayMetrics().density;
		return (int) (dpValue * scale + 0.5f);
	}

}
